package cn.edu.neusoft.zw725.foodorder.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.edu.neusoft.zw725.foodorder.R;
import cn.edu.neusoft.zw725.foodorder.bean.LoginBean;
import cn.edu.neusoft.zw725.foodorder.listener.LoginListener;
import cn.edu.neusoft.zw725.foodorder.model.LoginModel;

public class LoginActivity extends AppCompatActivity {
    private EditText ed_user;
    private EditText ed_pass;
    private Button btn_login;
    private Button btn_reg;
    private String username,userpass;
    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            if(loginBean.getUserid().equals("0")){
                Toast.makeText(LoginActivity.this,"登入失败",Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(LoginActivity.this,"登入成功",Toast.LENGTH_SHORT).show();
                saveUser();
                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(LoginActivity.this,"登入失败",Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        ed_user = (EditText)findViewById(R.id.ed_user);
        ed_pass = (EditText)findViewById(R.id.ed_pass);
        btn_login = (Button)findViewById(R.id.btn_login);
        btn_reg = (Button)findViewById(R.id.btn_reg);
        username = getUser_name();
        userpass = getUser_password();
        ed_user.setText(username);
        ed_pass.setText(userpass);

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(ed_user.getText().toString().trim().equals("")||ed_pass.getText().toString().equals("")){
                    Toast.makeText(LoginActivity.this,"用户名不能为空",Toast.LENGTH_SHORT).show();
                }
                else {
                    //拿着用户名和密码去服务器判断数据是否正确。
                    String username = ed_user.getText().toString();
                    String userpass = ed_pass.getText().toString();
                    LoginModel loginModel =new LoginModel();
                    loginModel.getLoginBean(username,userpass,loginListener);

                }
            }
        });

        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,RegActivity.class);
                startActivity(intent);
            }
        });
    }
    public void saveUser(){
        SharedPreferences sp = getSharedPreferences("userInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("username",ed_user.getText().toString());
        editor.putString("userpass",ed_pass.getText().toString());
        editor.commit();
    }
    public String getUser_name(){
        SharedPreferences sp;
        sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        return sp.getString("username","");
    }
    public String getUser_password(){
        SharedPreferences sp;
        sp = getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        return sp.getString("userpass","");
    }
}
